axios.defaults.baseURL = 'http://api-breakingnews-web.itheima.net/'

// 添加请求拦截器
axios.interceptors.request.use(
  function (config) {
    // 在发送请求之前做些什么
    // console.log('请求拦截器',config)
    // 配置请求头携带token。只有my的接口需要
    if (config.url.startsWith('/my')) {
      config.headers.Authorization = sessionStorage.getItem('bigEventToken')
    }
    return config
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error)
  }
)

// 添加响应拦截器
axios.interceptors.response.use(
  function (response) {
    // 对响应数据做点什么
    return response.data
  },
  function (error) {
    // 对响应错误做点什么
    return Promise.reject(error)
  }
)

// 注册请求
const reqRegisterPost = dataStr => {
  return axios({
    method: 'post',
    url: '/api/reguser',
    data: dataStr,
  })
}

// 登录请求
const reqLoginPost = dataStr => {
  return axios({
    method: 'post',
    url: '/api/login',
    data: dataStr,
  })
}

// 获取用户基本信息
const reqUserInfoGet = () => {
  return axios({
    method: 'get',
    url: '/my/userinfo',
  })
}

// 更新用户基本信息
const reqUpdateUserInfoPost = dataStr => {
  return axios({
    method: 'post',
    url: '/my/userinfo',
    data: dataStr,
  })
}

// 重置密码
const reqUpdatePwdPost = dataStr => {
  return axios({
    method: 'post',
    url: '/my/updatepwd',
    data: dataStr,
  })
}

// 更换头像
const reqUpdateAvatarPost = dataStr => {
  return axios({
    method: 'post',
    url: '/my/update/avatar',
    data: dataStr
  })
}

// 获取文章分类列表
const reqArticleCatesGet = () => {
  return axios({
    method: 'get',
    url: '/my/article/cates'
  })
}

// 新增文章分类
const reqAddArticleCatesPost = dataStr => {
  return axios({
    method: 'post',
    url: '/my/article/addcates',
    data: dataStr
  })
}

// 删除文章分类
const reqDeletCateGet = dataStr => {
  return axios({
    method: 'get',
    url: `/my/article/deletecate/${dataStr}`
  })
}

// 更新文章分类
const reqUpdateCatePost = dataStr => {
  return axios({
    method: 'post',
    url: '/my/article/updatecate',
    data: dataStr
  })
} 

// 获取文章列表
const reqArticleListGet = dataStr => {
  return axios({
    method: 'get',
    url: '/my/article/list',
    params: dataStr
  })
}

// 根据 Id 获取文章详情
const reqArticleInfoGet = dataStr => {
  return axios({
    method: 'get',
    url: `/my/article/${dataStr}`
  })
}

// 根据 Id 更新文章信息
const reqArticlEditPost = dataStr => {
  return axios({
    method: 'post',
    url: '/my/article/edit',
    data: dataStr
  })
}

// 发布新文章
const reqAddArticlePost = dataStr => {
  return axios({
    method: 'post',
    url: '/my/article/add',
    data: dataStr
  })
}